/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package client;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Amine
 */
public class Client {
    
    static final int PORT = 8888;
    static final int BUFFER_SIZE = 1024;
    
    public static void main(String[] args) {
        DatagramSocket clientSocket = null;
        DatagramPacket packet = null;
        InetAddress ipAdress = null;
        String json = null;
        InetAddress ip = null;  
        byte[] dataToSend = null;
        
        String location = args[0];
        float temperature = Float.parseFloat(args[1]);
        
        
        Random random = new Random();
        Gson gson = new GsonBuilder().create();
        
        while(true){
            float temperatureActuel = temperature + (float) random.nextInt(30) / 10;
            json = gson.toJson(new ReleveTemperature(new Date(), location, temperature));
            System.out.println(json);
            
            try {
                ip = InetAddress.getByName("localhost");
                clientSocket = new DatagramSocket();
                dataToSend = json.toString().getBytes();
                
                packet = new DatagramPacket(dataToSend, dataToSend.length, ip, PORT);
                clientSocket.send(packet);
                clientSocket.close();
            } catch (  UnknownHostException | SocketException  ex) {
                Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
            }
           
            
        }
        
        
    }
    
}
